package com.hnkywl.sts.service.tjbb;

import com.hnkywl.sts.dao.organ.LjwhDao;
import com.hnkywl.sts.dao.tjbb.LjsjllDao;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.util.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: xx
 * Date: 13-6-17
 * Time: 下午4:10
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LjsjllService {

    private LjsjllDao ljsjllDao;
    private LjwhDao ljwhDao;

    @Transactional
    public String findAllLjll(Long ljid, String timeFrom, String timeTo) {
        List<Map> list;
        Integer count = 0;
        String ljllJson = "";
        String footer = "";
        Double cgdje = 0.0; //采购单金额
        Double sumCgdje = 0.0; //总采购单金额
        Double xsdje = 0.0; //销售单金额
        Double sumXsdje = 0.0; //总销售单金额

        Double sjll = 0.0; //实际利率

        DecimalFormat df = new DecimalFormat("0.######"); //6位小数


        if (ljid > 0) {
            Lj lj = ljwhDao.get(ljid);
            list = ljsjllDao.getLjll(ljid, timeFrom, timeTo);
            if (null != list) {
                count = list.size();
                for (Map node : list) {
                    cgdje = 0.0;
                    xsdje = 0.0;
                    if (null != node.get("CGDJE")) {
                        cgdje = Double.parseDouble(node.get("CGDJE").toString());
                    }
                    if (null != node.get("XSDJE")) {
                        xsdje = Double.parseDouble(node.get("XSDJE").toString());
                    }
                    sumCgdje += cgdje;
                    sumXsdje += xsdje;

                    if (sumCgdje != 0) {
                        sjll = (sumXsdje - sumCgdje) / sumCgdje;
                    }

                    String strSjll = "0";
                    if (cgdje != 0) {
                        strSjll = df.format((xsdje - cgdje) / cgdje);
                    }

                    ljllJson += ",";
                    ljllJson += "{\"cgd\":\"" + node.get("CGDH") + "\", \"cgdje\":\"" + node.get("CGDJE") + "\", \"xsd\":\"" + node.get("XSDH") + "\", \"xsdje\":\"" + node.get("XSDJE") + "\", \"sjlr\":\"" + df.format(xsdje - cgdje) + "\", \"sjll\":\"" + strSjll + "\", \"ljll\":\"" + lj.getLl() + "\"}";
                }
            }

            footer = "{\"cgd\":\"统计:\", \"cgdje\":\"" + sumCgdje + "\", \"xsd\":\"\", \"xsdje\":\"" + sumXsdje + "\", \"sjlr\":\"" + df.format(sumXsdje - sumCgdje) + "\", \"sjll\":\"" + df.format(sjll) + "\", \"ljll\":\"" + lj.getLl() + "\"}";
        }

        //去掉第一个逗号
        if (ljllJson.length() > 1) {
            ljllJson = ljllJson.substring(1);
        }

        return "{\"total\":" + count + ",\"rows\":[" + ljllJson + "], \"footer\":[" + footer + "]}";
    }


    @Autowired
    public void setLjsjllDao(LjsjllDao ljsjllDao) {
        this.ljsjllDao = ljsjllDao;
    }

    @Autowired
    public void setLjwhDao(LjwhDao ljwhDao) {
        this.ljwhDao = ljwhDao;
    }
}
